package com.ineatconseil.yougo.beans;

import java.util.Calendar;
import java.util.List;
import java.util.Set;

import com.ineatconseil.yougo.utils.CustomCalendar;

/**
 * Represent a request to validate.<br>
 * This bean is use for render the "Validation list" page.
 * 
 * @author johanblo
 * 
 */
public class RequestToValidate {

	/**
	 * User's id.
	 */
	private Long id;

	/**
	 * User's fullname.
	 */
	private String fullName;

	/**
	 * The list of user's requests.
	 */
	private Set<Request> requests;

	/**
	 * The days.
	 */
	private List<DayInformation> days;

	public List<DayInformation> getDays() {
		return days;
	}

	public void setDays(List<DayInformation> days) {
		this.days = days;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public Set<Request> getRequests() {
		return requests;
	}

	public void setRequests(Set<Request> requests) {
		this.requests = requests;
	}

	/**
	 * Update days information.
	 * 
	 * @param year
	 *            the year.
	 * @param month
	 *            the month.
	 */
	public void calculateDayInformations(int year, int month) {

		CustomCalendar loader = new CustomCalendar(year, month);
		for (DayInformation dayInfo : loader.getDayInfo()) {

			for (Request req : this.getRequests()) {

				Calendar calFrom = Calendar.getInstance();
				calFrom.setTime(req.getFrom());

				Calendar calTo = Calendar.getInstance();
				calTo.setTime(req.getTo());

				if (calFrom.get(Calendar.YEAR) == year && calFrom.get(Calendar.MONTH) + 1 == month) {

					if (dayInfo.getDay() >= calFrom.get(Calendar.DATE) && dayInfo.getDay() <= calTo.get(Calendar.DATE)) {
						dayInfo.setRequestDay(true);
						dayInfo.setRequestId(req.getId());
						dayInfo.setUserId(id);
						dayInfo.setStatus(req.getStatus());

						if (calTo.get(Calendar.DATE) == dayInfo.getDay()) {
							dayInfo.setActionPossible(true);
						}

					} else {
						dayInfo.setRequestDay(false);
					}

				}

			}

		}

		this.days = loader.getDayInfo();

	}

}
